const uuid = require("uuid");

export function UUID() {
  return uuid.v4();
}

//是否是promise
export function isPromise(obj) {
  return (
    !!obj &&
    (typeof obj === "object" || typeof obj === "function") &&
    typeof obj.then === "function"
  );
}

//休眠方法
export const sleep = async (second) => {
  // eslint-disable-next-line no-unused-vars
  return new Promise((resolve, reject) => {
    window.setTimeout(() => {
      resolve();
    }, second);
  });
};

// eslint-disable-next-line no-unused-vars
export const animateCSS = (element, animation, prefix = "animate__") =>
  // We create a Promise and return it
  // eslint-disable-next-line no-unused-vars
  new Promise((resolve, reject) => {
    const animationName = `${prefix}${animation}`;
    const node = document.querySelector(element);

    node.classList.add(`${prefix}animated`, animationName);

    // When the animation ends, we clean the classes and resolve the Promise
    function handleAnimationEnd(event) {
      event.stopPropagation();
      node.classList.remove(`${prefix}animated`, animationName);
      resolve("Animation ended");
    }

    node.addEventListener("animationend", handleAnimationEnd, { once: true });
  });

//循环播放函数
export const playLoop = async (
  target,
  animation,
  component,
  duration = 2000
) => {
  while (component?.life ?? false) {
    await animateCSS(target, animation);
    await sleep(duration);
  }
};

//浅表克隆数组
export const shadowCloneArr = (arr) => {
  return [...arr];
};

/**
 * 向前移动(不改变原始数组)
 * @param {*} arr 数组
 * @param {*} index 移动元素的下标
 */
export const frontMove = (array, index) => {
  const arr = shadowCloneArr(array); //不改变原始数组
  if (index < 1) return arr;
  arr[index] = arr.splice(index - 1, 1, arr[index])[0];
  return arr;
};

/**
 * 向后移动(不改变原始数组)
 * @param {*} arr 数组
 * @param {*} index 移动元素的下标
 */
export const backMove = (array, index) => {
  const arr = shadowCloneArr(array); //不改变原始数组
  if (index >= arr.length - 1) return arr;
  arr[index] = arr.splice(index + 1, 1, arr[index])[0];
  return arr;
};

//移动到顶部(不改变原始数组)
export const topMove = (array, index) => {
  const arr = shadowCloneArr(array); //不改变原始数组
  const elements = arr.splice(index, 1);
  return [...arr, ...elements];
};

//移动到底部(不改变原始数组)
export const bottomMove = (array, index) => {
  const arr = shadowCloneArr(array); //不改变原始数组
  const elements = arr.splice(index, 1);
  return [...elements, ...arr];
};

//碰撞检测
export const collisionDetect = (shape, box) => {
  const intersected =
    shape.attrs.x > box.x() &&
    shape.attrs.x < box.x() + box.width() &&
    shape.attrs.y > box.y() &&
    shape.attrs.y < box.y() + box.height();

  // const intersected = !(
  //   box.attrs.x > shape.attrs.x + shape.attrs.width ||
  //   box.attrs.x + box.attrs.width < shape.attrs.x ||
  //   box.attrs.y > shape.attrs.y + shape.attrs.height ||
  //   box.attrs.y + box.attrs.height < shape.attrs.y
  // );
  return intersected;
};

//存储JSON文件
export const downloadJSON = (storageObj) => {
  const dataStr =
    "data:text/json;charset=utf-8," +
    encodeURIComponent(JSON.stringify(storageObj, null, 2));
  const dlAnchorElem = document.createElement("a");
  dlAnchorElem.setAttribute("href", dataStr);
  dlAnchorElem.setAttribute("download", "scene.json");
  dlAnchorElem.click();
};

//水平方向
export const horizontalLines = () => {
  let arr = [];
  const step = 20;
  const maxLength = 1000;
  const lineCount = 100;
  //0,0,100,0|0,10,100,10|0,20,100,20
  for (let i = 0; i < lineCount; i++) {
    arr.push({
      points: [0, step * i, maxLength, step * i],
      stroke: "#6D7B92",
      strokeWidth: 1,
    });
  }
  return arr;
};

//垂直方向
export const verticalLines = () => {
  let arr = [];
  const step = 20;
  const maxLength = 1000;
  const lineCount = 100;
  //0,0,0,100|10,0,10,100|20,0,20,100
  for (let i = 0; i < lineCount; i++) {
    arr.push({
      points: [step * i, 0, step * i, maxLength],
      stroke: "#6D7B92",
      strokeWidth: 1,
    });
  }
  return arr;
};

// 获取 当前 滚动条的长度， 水平 && 垂直方向
export function getScrollPosition() {
  let x, y;
  if (window.pageXOffset) {
    x = window.pageXOffset;
    y = window.pageYOffset;
  } else {
    x = (document.documentElement ?? document.body.parentNode ?? document.body)
      .scrollLeft;
    y = (document.documentElement ?? document.body.parentNode ?? document.body)
      .scrollTop;
  }
  return { x, y };
}
